﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.filters
{
    public class EncodeFrameFilter : NetFilter
    {
        public void invoke(object buffer, InvocationChain invocationChain)
        {
            if (!(buffer is byte[]))
            {
                invocationChain.invoke(buffer);
            }

            byte[] bytes = (byte[])buffer;

            byte[] result = new byte[bytes.Length + 6];


            result[0] = 0x11;
            result[1] = 0x22;
            result[2] = (byte)((bytes.Length&0xff00)>>8);
            result[3] = (byte)(bytes.Length&0xff);

            for (int i = 0; i < bytes.Length; i++)
            {
                result[i + 4] = bytes[i];
            }
            result[result.Length-2] = 0x55;
            result[result.Length-1] = 0x66;

            invocationChain.invoke(result);
        }
    }
}
